﻿<%@ WebHandler Language="C#" Class="RTGS.Web.ajaxhandler.usermanagement.sys_t_menu_rights" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using RTGS.Library;
using RTGS.Library.DataAccess.UserManagement;
using RTGS.Library.Entities.UserManagement;
using RTGS.Library.Enum;
using RTGS.Web.support;
namespace RTGS.Web.ajaxhandler.usermanagement
{
    public class sys_t_menu_rights : RTGS.Web.BaseHandler
    {
        private sysTMenuRightsRepo  menuRightsDal = new sysTMenuRightsRepo(ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString);
        private string MethodName = string.Empty;

        private sysTLogRepo logDal = new sysTLogRepo(ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString);
        private sysTLog log = new sysTLog();

        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);
    
            MethodName = context.Request.Params["method"];

            context.Response.Clear();
            context.Response.Buffer = true;

            switch (MethodName.ToLower())
            {
                case ("insert"):
                    context.Response.Write(Insert(context));
                    break;
                case ("getdatabyroleid"):
                   context.Response.Write(GetDataByRoleId(context));
                   break;
                default:
                    // do nothing
                    break;
            }

            context.Response.Flush();
            context.Response.End();

        }

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #region CRUD Handler

        private string Insert(HttpContext context)
        {
            JSONHelper helper = new JSONHelper();

            bool isSuccess = false;
            using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
            {
                string line = string.Empty;
                line = context.Server.UrlDecode(sr.ReadToEnd());

                var menu_rights = Newtonsoft.Json.JsonConvert.DeserializeObject<List<sysTMenuRights>>(line);

                menu_rights.ForEach(delegate(sysTMenuRights obj)
                {
                    obj.createdBy = base.UserLoggedIn;
                    obj.createdDate = DateTime.Now;
                });

                isSuccess = menuRightsDal.Save(menu_rights);
            }

            if (isSuccess)
            {
                helper.IsSuccess = true;
                helper.Message = "Data has been saved!";

                log = new sysTLog();
                log.logEvent = "Insert Menu Right Success";
                log.logMessage = string.Empty;
            }
            else
            {
                helper.IsSuccess = false;
                helper.Message = menuRightsDal.GetErrorMessage();

                log = new sysTLog();
                log.logEvent = "Insert Menu Right Error";
                log.logMessage = menuRightsDal.GetErrorMessage();
                
            }

            log.logSource = "sys_t_menu_rights.ashx";
            log.createdBy = base.UserLoggedIn;
            log.createdDate = DateTime.Now;
            logDal.Save(log);

            return Newtonsoft.Json.JsonConvert.SerializeObject(helper);

        }

        public string GetDataByRoleId(HttpContext context)
        {
            base.ProcessRequest(context);
            
            int roleId =0;
            if (context.Request.Params["roleId"] != null)
                roleId = Convert.ToInt32(context.Request.Params["roleId"]);

            List<sysTMenuRights> list = menuRightsDal.GetList();
            var result = list.Where(x=>x.roleId==roleId);

            return Newtonsoft.Json.JsonConvert.SerializeObject(result);

        }

        #endregion

    }
}